package com.appwoo.txtws.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.appwoo.txtws.activity.BookReaderQuestionActivity;
import com.appwoo.txtws.activity.BookstoreActivity;
import com.appwoo.txtws.bean.BookBean;
import com.appwoo.txtws.component.DBHelper;
import com.appwoo.txtws.component.Global;
import com.appwoo.txtws.component.Logger;
import com.appwoo.txtws.thread.BookStoreDownloadComplete;
import com.appwoo.txtws.thread.BookStoreDownloadPre;
import com.appwoo.txtws.thread.BookStoreDownloadTask;
import com.appwoo.txtws.wheel.widget.TextProgressBar;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BookstoreAdapter extends BaseAdapter
{
  private static final String TAG = BookstoreAdapter.class.getName();
  private int bookType;
  private Context context;
  private View.OnClickListener dialogNoOnClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      if ((BookstoreAdapter.this.downloadConfirmDialog != null) && (BookstoreAdapter.this.downloadConfirmDialog.isShowing()))
        BookstoreAdapter.this.downloadConfirmDialog.dismiss();
    }
  };
  private View.OnClickListener dialogYesOnClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramAnonymousView.getTag();
      if (localViewHolderBookStore != null)
      {
        BookstoreAdapter.this.download(localViewHolderBookStore);
        if ((BookstoreAdapter.this.downloadConfirmDialog != null) && (BookstoreAdapter.this.downloadConfirmDialog.isShowing()))
          BookstoreAdapter.this.downloadConfirmDialog.dismiss();
      }
    }
  };
  private Dialog downloadConfirmDialog;
  private int firstExtra;
  private String grade;
  private int height;
  private LayoutInflater inflater;
  private ArrayList<BookBean> listData;
  private DBHelper mDBHelper;
  private View.OnClickListener onDownloadClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramAnonymousView.getTag();
      if (!Global.isSDCard())
      {
        BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131427436));
        return;
      }
      if (1024 * (1024 * (-30 + Global.getSDCardSize(BookstoreAdapter.this.context))) < localViewHolderBookStore.book.getSize())
      {
        BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131427337));
        return;
      }
      if (localViewHolderBookStore.book.getDownloadPath() == null)
      {
        BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428181));
        return;
      }
      BookstoreAdapter.this.showConfirmDownloadDialog(localViewHolderBookStore);
    }
  };
  private View.OnClickListener onLayoutBookstoreInfoClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramAnonymousView.getTag();
      if (localViewHolderBookStore.book.getDownloadTime() > 0L)
      {
        BookstoreAdapter.this.openBook(localViewHolderBookStore.book);
        return;
      }
      BookstoreAdapter.this.readerOnline(localViewHolderBookStore.book);
    }
  };
  private View.OnClickListener onOpenClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramAnonymousView.getTag();
      BookstoreAdapter.this.openBook(localViewHolderBookStore.book);
    }
  };
  private View.OnClickListener onProgressClickListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramAnonymousView.getTag();
      BookstoreAdapter.this.downloadFail(localViewHolderBookStore);
    }
  };
  private ForegroundColorSpan redSpan1 = new ForegroundColorSpan(-65536);
  private ForegroundColorSpan redSpan2 = new ForegroundColorSpan(-65536);
  private String subject;

  public BookstoreAdapter(Context paramContext, ArrayList<BookBean> paramArrayList, int paramInt1, int paramInt2)
  {
    this.context = paramContext;
    this.listData = paramArrayList;
    this.inflater = ((LayoutInflater)this.context.getSystemService("layout_inflater"));
    this.mDBHelper = new DBHelper(this.context);
    this.height = paramInt1;
    this.firstExtra = paramInt2;
  }

  private void alertMessage(String paramString)
  {
    Toast.makeText(this.context, paramString, 0).show();
  }

  private void download(ViewHolderBookStore paramViewHolderBookStore)
  {
    showRightViewOfWait(paramViewHolderBookStore);
    File localFile = new File(Global.BOOK_PATH, paramViewHolderBookStore.book.getName());
    paramViewHolderBookStore.pbDownload.setProgress(0);
    BookStoreDownloadTask localBookStoreDownloadTask = new BookStoreDownloadTask(this.context, paramViewHolderBookStore.book, paramViewHolderBookStore.pbDownload, new BookStoreDownloadPreImpl(), new BookStoreDownloadCompleteImpl(paramViewHolderBookStore), paramViewHolderBookStore.book.getDownloadPath(), localFile.getPath(), this.bookType, this.subject, this.grade, paramViewHolderBookStore);
    BookstoreActivity.downloadTasks.add(localBookStoreDownloadTask);
    BookstoreActivity.executeNextAsyncTask();
  }

  private void downloadComplete(ViewHolderBookStore paramViewHolderBookStore)
  {
    BookstoreActivity.cancleAsyncTaskByBookname(paramViewHolderBookStore.book.getName());
    alertMessage(paramViewHolderBookStore.book.getName() + getString(2131428183));
    paramViewHolderBookStore.book = this.mDBHelper.getBookBeanByBookname(paramViewHolderBookStore.book.getName());
    this.listData.set(paramViewHolderBookStore.index, paramViewHolderBookStore.book);
    showRightViewOfOpen(paramViewHolderBookStore);
    BookstoreActivity.executeNextAsyncTask();
  }

  private void downloadFail(ViewHolderBookStore paramViewHolderBookStore)
  {
    BookstoreActivity.cancleAsyncTaskByBookname(paramViewHolderBookStore.book.getName());
    showRightViewOfDownload(paramViewHolderBookStore);
    BookstoreActivity.executeNextAsyncTask();
  }

  private String getString(int paramInt)
  {
    return this.context.getResources().getString(paramInt);
  }

  private String getStringSize(int paramInt)
  {
    String str = paramInt + "B";
    float f1 = paramInt / 1024.0F;
    if (f1 > 1.0F)
    {
      str = Math.round(f1 * 100.0F) / 100.0F + "K";
      float f2 = f1 / 1024.0F;
      if (f2 > 1.0F)
        str = Math.round(f2 * 100.0F) / 100.0F + "M";
    }
    return str;
  }

  private void openBook(BookBean paramBookBean)
  {
    Intent localIntent = new Intent(this.context, BookReaderQuestionActivity.class);
    localIntent.putExtra("bookname", paramBookBean.getName());
    localIntent.putExtra("readerType", "local");
    this.context.startActivity(localIntent);
  }

  private void readerOnline(BookBean paramBookBean)
  {
    Intent localIntent = new Intent(this.context, BookReaderQuestionActivity.class);
    localIntent.putExtra("bookname", paramBookBean.getName());
    localIntent.putExtra("downloadPath", paramBookBean.getDownloadPath());
    localIntent.putExtra("readerType", "web");
    localIntent.putExtra("grade", this.grade);
    this.context.startActivity(localIntent);
  }

  private void setRightView(BookBean paramBookBean, ViewHolderBookStore paramViewHolderBookStore)
  {
    BookBean localBookBean = this.mDBHelper.getBookBeanByBookname(paramBookBean.getName());
    int i = 0;
    if (localBookBean != null)
    {
      int j = localBookBean.getBookId();
      i = 0;
      if (j > 0)
      {
        paramViewHolderBookStore.book = localBookBean;
        this.listData.set(paramViewHolderBookStore.index, localBookBean);
        showRightViewOfOpen(paramViewHolderBookStore);
        i = 1;
      }
    }
    Iterator localIterator;
    if ((i == 0) && (BookstoreActivity.downloadTasks != null) && (!BookstoreActivity.downloadTasks.isEmpty()))
    {
      localIterator = BookstoreActivity.downloadTasks.iterator();
      if (localIterator.hasNext())
        break label108;
    }
    while (true)
    {
      if (i == 0)
        showRightViewOfDownload(paramViewHolderBookStore);
      return;
      label108: BookStoreDownloadTask localBookStoreDownloadTask = (BookStoreDownloadTask)localIterator.next();
      if (!localBookStoreDownloadTask.getBook().getName().equals(paramBookBean.getName()))
        break;
      if (localBookStoreDownloadTask.isRun())
      {
        showRightViewOfProgress(paramViewHolderBookStore);
        localBookStoreDownloadTask.setProgressBar(paramViewHolderBookStore.pbDownload);
        localBookStoreDownloadTask.setBookStoreDownloadComplete(new BookStoreDownloadCompleteImpl(paramViewHolderBookStore));
        i = 1;
      }
      else
      {
        showRightViewOfWait(paramViewHolderBookStore);
        localBookStoreDownloadTask.setObj(paramViewHolderBookStore);
        localBookStoreDownloadTask.setProgressBar(paramViewHolderBookStore.pbDownload);
        localBookStoreDownloadTask.setPre(new BookStoreDownloadPreImpl());
        localBookStoreDownloadTask.setBookStoreDownloadComplete(new BookStoreDownloadCompleteImpl(paramViewHolderBookStore));
        i = 1;
      }
    }
  }

  private void showConfirmDownloadDialog(ViewHolderBookStore paramViewHolderBookStore)
  {
    View localView = this.inflater.inflate(2130903050, null);
    ((TextView)localView.findViewById(2131492930)).setText(getString(2131428187));
    ((TextView)localView.findViewById(2131492931)).setText(getString(2131428180));
    Button localButton1 = (Button)localView.findViewById(2131492933);
    Button localButton2 = (Button)localView.findViewById(2131492934);
    localButton1.setTag(paramViewHolderBookStore);
    localButton1.setOnClickListener(this.dialogYesOnClickListener);
    localButton2.setOnClickListener(this.dialogNoOnClickListener);
    this.downloadConfirmDialog = new Dialog(this.context, 2131361833);
    this.downloadConfirmDialog.addContentView(localView, new LinearLayout.LayoutParams(-2, -2));
    this.downloadConfirmDialog.setCanceledOnTouchOutside(true);
    this.downloadConfirmDialog.show();
  }

  private void showRightViewOfDownload(ViewHolderBookStore paramViewHolderBookStore)
  {
    paramViewHolderBookStore.btnDownload.setVisibility(0);
    paramViewHolderBookStore.btnDownload.setOnClickListener(this.onDownloadClickListener);
    paramViewHolderBookStore.btnDownload.setText(getString(2131428155));
    paramViewHolderBookStore.btnOpen.setVisibility(8);
    paramViewHolderBookStore.pbDownload.setVisibility(8);
  }

  private void showRightViewOfOpen(ViewHolderBookStore paramViewHolderBookStore)
  {
    paramViewHolderBookStore.btnOpen.setVisibility(0);
    paramViewHolderBookStore.btnOpen.setOnClickListener(this.onOpenClickListener);
    paramViewHolderBookStore.btnDownload.setVisibility(8);
    paramViewHolderBookStore.pbDownload.setVisibility(8);
  }

  private void showRightViewOfProgress(ViewHolderBookStore paramViewHolderBookStore)
  {
    paramViewHolderBookStore.pbDownload.setVisibility(0);
    paramViewHolderBookStore.pbDownload.setOnClickListener(this.onProgressClickListener);
    paramViewHolderBookStore.btnDownload.setVisibility(8);
    paramViewHolderBookStore.btnOpen.setVisibility(8);
  }

  private void showRightViewOfWait(ViewHolderBookStore paramViewHolderBookStore)
  {
    paramViewHolderBookStore.btnDownload.setVisibility(0);
    paramViewHolderBookStore.btnDownload.setOnClickListener(this.onProgressClickListener);
    paramViewHolderBookStore.btnDownload.setText(getString(2131428159));
    paramViewHolderBookStore.btnOpen.setVisibility(8);
    paramViewHolderBookStore.pbDownload.setVisibility(8);
  }

  public void addData(ArrayList<BookBean> paramArrayList)
  {
    if ((paramArrayList == null) || (paramArrayList.isEmpty()));
    while (true)
    {
      return;
      Iterator localIterator = paramArrayList.iterator();
      while (localIterator.hasNext())
      {
        BookBean localBookBean = (BookBean)localIterator.next();
        this.listData.add(localBookBean);
      }
    }
  }

  public void clearListData()
  {
    if ((this.listData != null) && (!this.listData.isEmpty()))
      this.listData.clear();
  }

  public int getCount()
  {
    return this.listData.size();
  }

  public Object getItem(int paramInt)
  {
    return null;
  }

  public long getItemId(int paramInt)
  {
    return 0L;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    ViewHolderBookStore localViewHolderBookStore;
    BookBean localBookBean;
    if (paramView == null)
    {
      localViewHolderBookStore = new ViewHolderBookStore();
      paramView = this.inflater.inflate(2130903064, null);
      localViewHolderBookStore.tvBookstoreBookname = ((TextView)paramView.findViewById(2131492989));
      localViewHolderBookStore.tvBookstoreBooksize = ((TextView)paramView.findViewById(2131492991));
      localViewHolderBookStore.btnOpen = ((Button)paramView.findViewById(2131492993));
      localViewHolderBookStore.btnDownload = ((Button)paramView.findViewById(2131492992));
      localViewHolderBookStore.layoutBookstoreInfo = ((LinearLayout)paramView.findViewById(2131492988));
      localViewHolderBookStore.pbDownload = ((TextProgressBar)paramView.findViewById(2131492994));
      localViewHolderBookStore.tvBookstoreBooksizeText = ((TextView)paramView.findViewById(2131492990));
      paramView.setTag(localViewHolderBookStore);
      localBookBean = (BookBean)this.listData.get(paramInt);
      if (paramInt != 0)
        break label328;
      paramView.setLayoutParams(new AbsListView.LayoutParams(-1, this.height + this.firstExtra));
    }
    while (true)
    {
      if (localBookBean == null)
        break label347;
      localViewHolderBookStore.book = localBookBean;
      localViewHolderBookStore.tvBookstoreBookname.setVisibility(0);
      localViewHolderBookStore.tvBookstoreBooksize.setVisibility(0);
      localViewHolderBookStore.tvBookstoreBooksizeText.setVisibility(0);
      localViewHolderBookStore.tvBookstoreBookname.setText(localBookBean.getName());
      String str = getStringSize(localBookBean.getSize());
      localViewHolderBookStore.tvBookstoreBooksize.setText(str);
      localViewHolderBookStore.layoutBookstoreInfo.setTag(localViewHolderBookStore);
      localViewHolderBookStore.layoutBookstoreInfo.setOnClickListener(this.onLayoutBookstoreInfoClickListener);
      localViewHolderBookStore.btnOpen.setTag(localViewHolderBookStore);
      localViewHolderBookStore.btnDownload.setTag(localViewHolderBookStore);
      localViewHolderBookStore.pbDownload.setTag(localViewHolderBookStore);
      localViewHolderBookStore.index = paramInt;
      setRightView(localBookBean, localViewHolderBookStore);
      return paramView;
      localViewHolderBookStore = (ViewHolderBookStore)paramView.getTag();
      break;
      label328: paramView.setLayoutParams(new AbsListView.LayoutParams(-1, this.height));
    }
    label347: localViewHolderBookStore.tvBookstoreBookname.setVisibility(8);
    localViewHolderBookStore.tvBookstoreBooksize.setVisibility(8);
    localViewHolderBookStore.btnOpen.setVisibility(8);
    localViewHolderBookStore.btnDownload.setVisibility(8);
    localViewHolderBookStore.pbDownload.setVisibility(8);
    localViewHolderBookStore.tvBookstoreBooksizeText.setVisibility(8);
    localViewHolderBookStore.layoutBookstoreInfo.setOnClickListener(null);
    return paramView;
  }

  public void setBookType(int paramInt)
  {
    this.bookType = paramInt;
  }

  public void setData(ArrayList<BookBean> paramArrayList)
  {
    paramArrayList.clear();
    addData(paramArrayList);
  }

  public void setGrade(String paramString)
  {
    this.grade = paramString;
  }

  public SpannableString setRedLightText(String paramString1, String paramString2, String paramString3)
  {
    SpannableString localSpannableString = new SpannableString(paramString1);
    Matcher localMatcher2;
    Matcher localMatcher1;
    if ((paramString2 != null) && (!"".equals(paramString2)))
    {
      localMatcher2 = Pattern.compile(paramString2).matcher(localSpannableString);
      if (localMatcher2.find());
    }
    else if ((paramString3 != null) && (!"".equals(paramString3)))
    {
      localMatcher1 = Pattern.compile(paramString3).matcher(localSpannableString);
    }
    while (true)
    {
      if (!localMatcher1.find())
      {
        return localSpannableString;
        int k = localMatcher2.start();
        int m = localMatcher2.end();
        localSpannableString.setSpan(this.redSpan1, k, m, 33);
        break;
      }
      int i = localMatcher1.start();
      int j = localMatcher1.end();
      localSpannableString.setSpan(this.redSpan2, i, j, 33);
    }
  }

  public void setSubject(String paramString)
  {
    this.subject = paramString;
  }

  class BookStoreDownloadCompleteImpl
    implements BookStoreDownloadComplete
  {
    BookstoreAdapter.ViewHolderBookStore viewHolder;

    public BookStoreDownloadCompleteImpl(BookstoreAdapter.ViewHolderBookStore arg2)
    {
      Object localObject;
      this.viewHolder = localObject;
    }

    public void complete(String paramString1, int paramInt, String paramString2)
    {
      "5".equals(paramString1);
      if ("0".equals(paramString1))
      {
        BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428182));
        Logger.e(BookstoreAdapter.TAG, "下载缺少数据");
        BookstoreAdapter.this.downloadFail(this.viewHolder);
      }
      do
      {
        return;
        if ("2".equals(paramString1))
        {
          BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428181));
          Logger.e(BookstoreAdapter.TAG, "下载地址错误");
          BookstoreAdapter.this.downloadFail(this.viewHolder);
          return;
        }
        if ("3".equals(paramString1))
        {
          BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428181));
          Logger.e(BookstoreAdapter.TAG, "服务器文件错误");
          BookstoreAdapter.this.downloadFail(this.viewHolder);
          return;
        }
        if ("1".equals(paramString1))
        {
          BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131427515));
          Logger.e(BookstoreAdapter.TAG, "网络连接失败");
          BookstoreAdapter.this.downloadFail(this.viewHolder);
          return;
        }
        if ("4".equals(paramString1))
        {
          BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428184));
          Logger.e(BookstoreAdapter.TAG, "已取消下载");
          BookstoreAdapter.this.downloadFail(this.viewHolder);
          return;
        }
        if ("6".equals(paramString1))
        {
          BookstoreAdapter.this.alertMessage(BookstoreAdapter.access$2(BookstoreAdapter.this, 2131428186));
          Logger.e(BookstoreAdapter.TAG, "创建本地文件失败");
          BookstoreAdapter.this.downloadFail(this.viewHolder);
          return;
        }
      }
      while (!"20".equals(paramString1));
      BookstoreAdapter.this.downloadComplete(this.viewHolder);
    }
  }

  class BookStoreDownloadPreImpl
    implements BookStoreDownloadPre
  {
    BookStoreDownloadPreImpl()
    {
    }

    public void onPreExecute(Object paramObject)
    {
      BookstoreAdapter.ViewHolderBookStore localViewHolderBookStore = (BookstoreAdapter.ViewHolderBookStore)paramObject;
      BookstoreAdapter.this.showRightViewOfProgress(localViewHolderBookStore);
    }
  }

  class ViewHolderBookStore
  {
    BookBean book;
    Button btnDownload;
    Button btnOpen;
    int index;
    LinearLayout layoutBookstoreInfo;
    TextProgressBar pbDownload;
    TextView tvBookstoreBookname;
    TextView tvBookstoreBooksize;
    TextView tvBookstoreBooksizeText;

    ViewHolderBookStore()
    {
    }
  }
}

/* Location:           C:\Documents and Settings\Administrator\classes_dex2jar.jar
 * Qualified Name:     com.appwoo.txtws.adapter.BookstoreAdapter
 * JD-Core Version:    0.6.2
 */